{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "59520552",
   "metadata": {},
   "source": [
    "# face++实践 \n",
    "## 人脸检测"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "61441ecc",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1.导入需要的requests模块\n",
    "import requests\n",
    "\n",
    "# 2.输入我们需要API（face++）网站注册的API_Key\\Secret\n",
    "\n",
    "API_Key = \"aZW8w-JCfLUxYnTwQQu_RKLAUduXKvTW\"\n",
    "API_Secret = \"wmAcTGRDAkP1JB4zYe0H3Q15mn1UCMxC\"\n",
    "\n",
    "# 3.准备base URL(face detect) API测试接口的url\n",
    "base_url = \"https://api-cn.faceplusplus.com/facepp/v3/detect\"\n",
    "\n",
    "# 4.请求方法：POST\n",
    "\n",
    "# 5.准备请求参数：payload\n",
    "payload = {\n",
    "    \"api_key\":API_Key,\n",
    "    \"api_secret\":API_Secret,\n",
    "    \"image_url\":\"http://thumb10.jfcdns.com/thumb/up/2017-8/201782115518958221215010_600_0.jpg\",\n",
    "    \"return_landmark\":1,\n",
    "    \"return_attributes\":\"gender,age,smiling\"\n",
    "\n",
    "}\n",
    "# 6.请求数据\n",
    "\n",
    "r = requests.post(base_url,payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "0276d4ed",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "0c57e342",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647314278,8aad4390-aed7-46d6-83e5-a3ada3bafa02',\n",
       " 'time_used': 237,\n",
       " 'faces': [{'face_token': '371a059c1c2fceaa1b72c4539b8be469',\n",
       "   'face_rectangle': {'top': 132, 'left': 178, 'width': 174, 'height': 174},\n",
       "   'landmark': {'contour_chin': {'x': 268, 'y': 305},\n",
       "    'contour_left1': {'x': 176, 'y': 167},\n",
       "    'contour_left2': {'x': 178, 'y': 187},\n",
       "    'contour_left3': {'x': 182, 'y': 206},\n",
       "    'contour_left4': {'x': 188, 'y': 225},\n",
       "    'contour_left5': {'x': 193, 'y': 244},\n",
       "    'contour_left6': {'x': 202, 'y': 262},\n",
       "    'contour_left7': {'x': 213, 'y': 278},\n",
       "    'contour_left8': {'x': 228, 'y': 291},\n",
       "    'contour_left9': {'x': 246, 'y': 302},\n",
       "    'contour_right1': {'x': 350, 'y': 159},\n",
       "    'contour_right2': {'x': 350, 'y': 179},\n",
       "    'contour_right3': {'x': 348, 'y': 199},\n",
       "    'contour_right4': {'x': 345, 'y': 219},\n",
       "    'contour_right5': {'x': 341, 'y': 238},\n",
       "    'contour_right6': {'x': 333, 'y': 257},\n",
       "    'contour_right7': {'x': 322, 'y': 273},\n",
       "    'contour_right8': {'x': 308, 'y': 288},\n",
       "    'contour_right9': {'x': 291, 'y': 300},\n",
       "    'left_eye_bottom': {'x': 220, 'y': 173},\n",
       "    'left_eye_center': {'x': 221, 'y': 167},\n",
       "    'left_eye_left_corner': {'x': 202, 'y': 168},\n",
       "    'left_eye_lower_left_quarter': {'x': 210, 'y': 172},\n",
       "    'left_eye_lower_right_quarter': {'x': 232, 'y': 171},\n",
       "    'left_eye_pupil': {'x': 222, 'y': 164},\n",
       "    'left_eye_right_corner': {'x': 242, 'y': 169},\n",
       "    'left_eye_top': {'x': 220, 'y': 158},\n",
       "    'left_eye_upper_left_quarter': {'x': 210, 'y': 161},\n",
       "    'left_eye_upper_right_quarter': {'x': 232, 'y': 161},\n",
       "    'left_eyebrow_left_corner': {'x': 187, 'y': 146},\n",
       "    'left_eyebrow_lower_left_quarter': {'x': 199, 'y': 145},\n",
       "    'left_eyebrow_lower_middle': {'x': 213, 'y': 146},\n",
       "    'left_eyebrow_lower_right_quarter': {'x': 227, 'y': 149},\n",
       "    'left_eyebrow_right_corner': {'x': 243, 'y': 151},\n",
       "    'left_eyebrow_upper_left_quarter': {'x': 198, 'y': 137},\n",
       "    'left_eyebrow_upper_middle': {'x': 214, 'y': 138},\n",
       "    'left_eyebrow_upper_right_quarter': {'x': 229, 'y': 142},\n",
       "    'mouth_left_corner': {'x': 232, 'y': 251},\n",
       "    'mouth_lower_lip_bottom': {'x': 267, 'y': 267},\n",
       "    'mouth_lower_lip_left_contour1': {'x': 249, 'y': 253},\n",
       "    'mouth_lower_lip_left_contour2': {'x': 241, 'y': 259},\n",
       "    'mouth_lower_lip_left_contour3': {'x': 252, 'y': 266},\n",
       "    'mouth_lower_lip_right_contour1': {'x': 282, 'y': 251},\n",
       "    'mouth_lower_lip_right_contour2': {'x': 290, 'y': 256},\n",
       "    'mouth_lower_lip_right_contour3': {'x': 280, 'y': 264},\n",
       "    'mouth_lower_lip_top': {'x': 266, 'y': 253},\n",
       "    'mouth_right_corner': {'x': 298, 'y': 247},\n",
       "    'mouth_upper_lip_bottom': {'x': 266, 'y': 251},\n",
       "    'mouth_upper_lip_left_contour1': {'x': 258, 'y': 241},\n",
       "    'mouth_upper_lip_left_contour2': {'x': 245, 'y': 245},\n",
       "    'mouth_upper_lip_left_contour3': {'x': 250, 'y': 251},\n",
       "    'mouth_upper_lip_right_contour1': {'x': 274, 'y': 240},\n",
       "    'mouth_upper_lip_right_contour2': {'x': 286, 'y': 243},\n",
       "    'mouth_upper_lip_right_contour3': {'x': 282, 'y': 249},\n",
       "    'mouth_upper_lip_top': {'x': 266, 'y': 243},\n",
       "    'nose_contour_left1': {'x': 251, 'y': 167},\n",
       "    'nose_contour_left2': {'x': 248, 'y': 204},\n",
       "    'nose_contour_left3': {'x': 255, 'y': 226},\n",
       "    'nose_contour_lower_middle': {'x': 266, 'y': 228},\n",
       "    'nose_contour_right1': {'x': 274, 'y': 166},\n",
       "    'nose_contour_right2': {'x': 280, 'y': 202},\n",
       "    'nose_contour_right3': {'x': 277, 'y': 224},\n",
       "    'nose_left': {'x': 245, 'y': 220},\n",
       "    'nose_right': {'x': 285, 'y': 217},\n",
       "    'nose_tip': {'x': 265, 'y': 213},\n",
       "    'right_eye_bottom': {'x': 305, 'y': 169},\n",
       "    'right_eye_center': {'x': 303, 'y': 163},\n",
       "    'right_eye_left_corner': {'x': 283, 'y': 166},\n",
       "    'right_eye_lower_left_quarter': {'x': 294, 'y': 168},\n",
       "    'right_eye_lower_right_quarter': {'x': 314, 'y': 167},\n",
       "    'right_eye_pupil': {'x': 303, 'y': 160},\n",
       "    'right_eye_right_corner': {'x': 321, 'y': 162},\n",
       "    'right_eye_top': {'x': 303, 'y': 154},\n",
       "    'right_eye_upper_left_quarter': {'x': 292, 'y': 158},\n",
       "    'right_eye_upper_right_quarter': {'x': 314, 'y': 156},\n",
       "    'right_eyebrow_left_corner': {'x': 277, 'y': 149},\n",
       "    'right_eyebrow_lower_left_quarter': {'x': 292, 'y': 146},\n",
       "    'right_eyebrow_lower_middle': {'x': 306, 'y': 142},\n",
       "    'right_eyebrow_lower_right_quarter': {'x': 320, 'y': 138},\n",
       "    'right_eyebrow_right_corner': {'x': 333, 'y': 137},\n",
       "    'right_eyebrow_upper_left_quarter': {'x': 290, 'y': 140},\n",
       "    'right_eyebrow_upper_middle': {'x': 304, 'y': 134},\n",
       "    'right_eyebrow_upper_right_quarter': {'x': 319, 'y': 131}},\n",
       "   'attributes': {'gender': {'value': 'Female'},\n",
       "    'age': {'value': 23},\n",
       "    'smile': {'value': 0.004, 'threshold': 50.0}}}],\n",
       " 'image_id': 'iGkOO0vIVdOtC8bDQWAVmA==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "48ddb103",
   "metadata": {},
   "source": [
    "## 人脸分析 API"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "5fdf525b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1.导入需要的requests模块\n",
    "import requests\n",
    "\n",
    "# 2.输入我们需要API（face++）网站注册的API_Key\\Secret\n",
    "\n",
    "API_Key = \"aZW8w-JCfLUxYnTwQQu_RKLAUduXKvTW\"\n",
    "API_Secret = \"wmAcTGRDAkP1JB4zYe0H3Q15mn1UCMxC\"\n",
    "\n",
    "# 3.准备base URL(face detect) API测试接口的url\n",
    "base_url = \"https://api-cn.faceplusplus.com/facepp/v3/face/analyze\"\n",
    "\n",
    "# 4.请求方法：POST\n",
    "\n",
    "# 5.准备请求参数：payload\n",
    "payload = {\n",
    "    \"api_key\":API_Key,\n",
    "    \"api_secret\":API_Secret,\n",
    "    \"face_tokens\":'ede4e42e62954a8439844d49ce50fc9e',\n",
    "    \"image_url\":\"http://thumb10.jfcdns.com/thumb/up/2017-8/201782115518958221215010_600_0.jpg\",\n",
    "    \"return_landmark\":1,\n",
    "    \"return_attributes\":\"gender,age,smiling\"\n",
    "\n",
    "}\n",
    "# 6.请求数据\n",
    "\n",
    "r = requests.post(base_url,payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "13bd2763",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "772446f7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 133,\n",
       " 'request_id': '1647314320,0b049534-9e91-4500-a984-624d6d1ef21b',\n",
       " 'faces': [{'landmark': {'mouth_upper_lip_left_contour2': {'y': 247, 'x': 244},\n",
       "    'mouth_upper_lip_top': {'y': 245, 'x': 265},\n",
       "    'mouth_upper_lip_left_contour1': {'y': 243, 'x': 257},\n",
       "    'left_eye_upper_left_quarter': {'y': 160, 'x': 211},\n",
       "    'left_eyebrow_lower_middle': {'y': 148, 'x': 212},\n",
       "    'mouth_upper_lip_left_contour3': {'y': 252, 'x': 249},\n",
       "    'left_eyebrow_lower_left_quarter': {'y': 148, 'x': 198},\n",
       "    'left_eye_bottom': {'y': 172, 'x': 220},\n",
       "    'right_eyebrow_lower_left_quarter': {'y': 147, 'x': 293},\n",
       "    'nose_contour_left1': {'y': 168, 'x': 252},\n",
       "    'mouth_lower_lip_right_contour1': {'y': 253, 'x': 280},\n",
       "    'mouth_lower_lip_left_contour2': {'y': 260, 'x': 241},\n",
       "    'mouth_lower_lip_left_contour3': {'y': 266, 'x': 251},\n",
       "    'mouth_lower_lip_bottom': {'y': 268, 'x': 265},\n",
       "    'contour_chin': {'y': 305, 'x': 268},\n",
       "    'contour_left9': {'y': 302, 'x': 247},\n",
       "    'mouth_lower_lip_top': {'y': 256, 'x': 265},\n",
       "    'right_eyebrow_upper_middle': {'y': 134, 'x': 306},\n",
       "    'left_eyebrow_left_corner': {'y': 148, 'x': 186},\n",
       "    'contour_left6': {'y': 261, 'x': 204},\n",
       "    'right_eye_lower_right_quarter': {'y': 165, 'x': 314},\n",
       "    'right_eye_bottom': {'y': 168, 'x': 304},\n",
       "    'contour_left7': {'y': 277, 'x': 216},\n",
       "    'contour_right8': {'y': 288, 'x': 306},\n",
       "    'contour_left5': {'y': 244, 'x': 196},\n",
       "    'contour_left4': {'y': 225, 'x': 190},\n",
       "    'contour_left3': {'y': 206, 'x': 186},\n",
       "    'contour_left2': {'y': 187, 'x': 183},\n",
       "    'contour_left1': {'y': 168, 'x': 182},\n",
       "    'left_eye_lower_left_quarter': {'y': 169, 'x': 210},\n",
       "    'contour_right1': {'y': 162, 'x': 348},\n",
       "    'right_eye_pupil': {'y': 161, 'x': 302},\n",
       "    'contour_right2': {'y': 182, 'x': 348},\n",
       "    'mouth_left_corner': {'y': 251, 'x': 232},\n",
       "    'contour_right4': {'y': 220, 'x': 343},\n",
       "    'contour_right7': {'y': 273, 'x': 320},\n",
       "    'contour_left8': {'y': 291, 'x': 230},\n",
       "    'nose_right': {'y': 216, 'x': 285},\n",
       "    'right_eye_upper_right_quarter': {'y': 156, 'x': 312},\n",
       "    'nose_tip': {'y': 215, 'x': 265},\n",
       "    'contour_right5': {'y': 239, 'x': 338},\n",
       "    'nose_contour_lower_middle': {'y': 227, 'x': 266},\n",
       "    'right_eye_top': {'y': 155, 'x': 302},\n",
       "    'right_eye_right_corner': {'y': 160, 'x': 321},\n",
       "    'mouth_upper_lip_right_contour1': {'y': 242, 'x': 273},\n",
       "    'mouth_upper_lip_right_contour2': {'y': 245, 'x': 285},\n",
       "    'right_eyebrow_lower_right_quarter': {'y': 142, 'x': 322},\n",
       "    'contour_right9': {'y': 300, 'x': 290},\n",
       "    'mouth_right_corner': {'y': 249, 'x': 296},\n",
       "    'right_eye_lower_left_quarter': {'y': 168, 'x': 295},\n",
       "    'left_eyebrow_right_corner': {'y': 151, 'x': 241},\n",
       "    'left_eyebrow_lower_right_quarter': {'y': 150, 'x': 226},\n",
       "    'right_eye_center': {'y': 163, 'x': 303},\n",
       "    'left_eye_pupil': {'y': 164, 'x': 222},\n",
       "    'left_eye_upper_right_quarter': {'y': 161, 'x': 230},\n",
       "    'nose_left': {'y': 218, 'x': 246},\n",
       "    'right_eyebrow_lower_middle': {'y': 144, 'x': 307},\n",
       "    'left_eye_center': {'y': 166, 'x': 220},\n",
       "    'left_eye_lower_right_quarter': {'y': 171, 'x': 229},\n",
       "    'right_eyebrow_left_corner': {'y': 149, 'x': 278},\n",
       "    'right_eye_left_corner': {'y': 167, 'x': 286},\n",
       "    'left_eyebrow_upper_left_quarter': {'y': 139, 'x': 197},\n",
       "    'left_eye_left_corner': {'y': 165, 'x': 203},\n",
       "    'left_eye_right_corner': {'y': 169, 'x': 238},\n",
       "    'right_eyebrow_upper_left_quarter': {'y': 139, 'x': 290},\n",
       "    'right_eyebrow_right_corner': {'y': 142, 'x': 335},\n",
       "    'right_eye_upper_left_quarter': {'y': 159, 'x': 293},\n",
       "    'left_eyebrow_upper_middle': {'y': 138, 'x': 212},\n",
       "    'right_eyebrow_upper_right_quarter': {'y': 133, 'x': 322},\n",
       "    'mouth_lower_lip_right_contour3': {'y': 265, 'x': 279},\n",
       "    'nose_contour_left2': {'y': 205, 'x': 249},\n",
       "    'nose_contour_left3': {'y': 223, 'x': 255},\n",
       "    'nose_contour_right1': {'y': 167, 'x': 272},\n",
       "    'nose_contour_right2': {'y': 202, 'x': 280},\n",
       "    'mouth_lower_lip_right_contour2': {'y': 258, 'x': 289},\n",
       "    'mouth_upper_lip_bottom': {'y': 252, 'x': 265},\n",
       "    'contour_right6': {'y': 257, 'x': 330},\n",
       "    'nose_contour_right3': {'y': 222, 'x': 276},\n",
       "    'left_eye_top': {'y': 159, 'x': 220},\n",
       "    'mouth_upper_lip_right_contour3': {'y': 251, 'x': 280},\n",
       "    'left_eyebrow_upper_right_quarter': {'y': 142, 'x': 227},\n",
       "    'contour_right3': {'y': 201, 'x': 346},\n",
       "    'mouth_lower_lip_left_contour1': {'y': 254, 'x': 249}},\n",
       "   'attributes': {'gender': {'value': 'Female'},\n",
       "    'age': {'value': 26},\n",
       "    'smile': {'threshold': 50.0, 'value': 0.004}},\n",
       "   'face_rectangle': {'width': 174, 'top': 132, 'left': 178, 'height': 174},\n",
       "   'face_token': 'ede4e42e62954a8439844d49ce50fc9e'}]}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "023c09b0",
   "metadata": {},
   "source": [
    "## 人脸对比"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "2480e0df",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1.导入需要的requests模块\n",
    "import requests\n",
    "\n",
    "# 2.输入我们需要API（face++）网站注册的API_Key\\Secret\n",
    "\n",
    "API_Key = \"aZW8w-JCfLUxYnTwQQu_RKLAUduXKvTW\"\n",
    "API_Secret = \"wmAcTGRDAkP1JB4zYe0H3Q15mn1UCMxC\"\n",
    "\n",
    "# 3.准备base URL(face detect) API测试接口的url\n",
    "base_url = \"https://api-cn.faceplusplus.com/facepp/v3/detect\"\n",
    "\n",
    "# 4.请求方法：POST\n",
    "\n",
    "# 5.准备请求参数：payload\n",
    "payload = {\n",
    "    \"api_key\":API_Key,\n",
    "    \"api_secret\":API_Secret,\n",
    "    \"image_url\":\"https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fwww.pianshen.com%2Fimages%2F251%2Ffc5604a75a9b9d18f992b57b1cd426b3.JPEG&refer=http%3A%2F%2Fwww.pianshen.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1649907078&t=55d971230617c85c2719bdbd11855578\",\n",
    "    \"return_landmark\":1,\n",
    "    \"return_attributes\":\"gender,age,smiling\"\n",
    "\n",
    "}\n",
    "# 6.请求数据\n",
    "\n",
    "r = requests.post(base_url,payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "05120d24",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "9fb108aa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647315138,8acc7e1e-86ac-423b-af02-fcb971bed4f1',\n",
       " 'time_used': 330,\n",
       " 'faces': [{'face_token': 'ce2c77d7c093ebbd320a8bdfa7ed8340',\n",
       "   'face_rectangle': {'top': 401, 'left': 261, 'width': 529, 'height': 529},\n",
       "   'landmark': {'contour_chin': {'x': 507, 'y': 921},\n",
       "    'contour_left1': {'x': 261, 'y': 484},\n",
       "    'contour_left2': {'x': 265, 'y': 544},\n",
       "    'contour_left3': {'x': 277, 'y': 602},\n",
       "    'contour_left4': {'x': 290, 'y': 660},\n",
       "    'contour_left5': {'x': 303, 'y': 718},\n",
       "    'contour_left6': {'x': 323, 'y': 775},\n",
       "    'contour_left7': {'x': 353, 'y': 825},\n",
       "    'contour_left8': {'x': 392, 'y': 870},\n",
       "    'contour_left9': {'x': 442, 'y': 905},\n",
       "    'contour_right1': {'x': 790, 'y': 488},\n",
       "    'contour_right2': {'x': 788, 'y': 551},\n",
       "    'contour_right3': {'x': 779, 'y': 612},\n",
       "    'contour_right4': {'x': 767, 'y': 674},\n",
       "    'contour_right5': {'x': 752, 'y': 735},\n",
       "    'contour_right6': {'x': 724, 'y': 792},\n",
       "    'contour_right7': {'x': 686, 'y': 841},\n",
       "    'contour_right8': {'x': 638, 'y': 882},\n",
       "    'contour_right9': {'x': 578, 'y': 912},\n",
       "    'left_eye_bottom': {'x': 381, 'y': 501},\n",
       "    'left_eye_center': {'x': 384, 'y': 487},\n",
       "    'left_eye_left_corner': {'x': 330, 'y': 488},\n",
       "    'left_eye_lower_left_quarter': {'x': 353, 'y': 497},\n",
       "    'left_eye_lower_right_quarter': {'x': 413, 'y': 498},\n",
       "    'left_eye_pupil': {'x': 384, 'y': 476},\n",
       "    'left_eye_right_corner': {'x': 443, 'y': 495},\n",
       "    'left_eye_top': {'x': 384, 'y': 463},\n",
       "    'left_eye_upper_left_quarter': {'x': 353, 'y': 469},\n",
       "    'left_eye_upper_right_quarter': {'x': 416, 'y': 472},\n",
       "    'left_eyebrow_left_corner': {'x': 278, 'y': 446},\n",
       "    'left_eyebrow_lower_left_quarter': {'x': 320, 'y': 441},\n",
       "    'left_eyebrow_lower_middle': {'x': 363, 'y': 444},\n",
       "    'left_eyebrow_lower_right_quarter': {'x': 408, 'y': 453},\n",
       "    'left_eyebrow_right_corner': {'x': 455, 'y': 457},\n",
       "    'left_eyebrow_upper_left_quarter': {'x': 317, 'y': 412},\n",
       "    'left_eyebrow_upper_middle': {'x': 368, 'y': 411},\n",
       "    'left_eyebrow_upper_right_quarter': {'x': 415, 'y': 425},\n",
       "    'mouth_left_corner': {'x': 415, 'y': 753},\n",
       "    'mouth_lower_lip_bottom': {'x': 510, 'y': 797},\n",
       "    'mouth_lower_lip_left_contour1': {'x': 462, 'y': 754},\n",
       "    'mouth_lower_lip_left_contour2': {'x': 439, 'y': 775},\n",
       "    'mouth_lower_lip_left_contour3': {'x': 470, 'y': 791},\n",
       "    'mouth_lower_lip_right_contour1': {'x': 557, 'y': 753},\n",
       "    'mouth_lower_lip_right_contour2': {'x': 581, 'y': 775},\n",
       "    'mouth_lower_lip_right_contour3': {'x': 550, 'y': 791},\n",
       "    'mouth_lower_lip_top': {'x': 509, 'y': 755},\n",
       "    'mouth_right_corner': {'x': 606, 'y': 754},\n",
       "    'mouth_upper_lip_bottom': {'x': 509, 'y': 758},\n",
       "    'mouth_upper_lip_left_contour1': {'x': 484, 'y': 727},\n",
       "    'mouth_upper_lip_left_contour2': {'x': 447, 'y': 737},\n",
       "    'mouth_upper_lip_left_contour3': {'x': 462, 'y': 755},\n",
       "    'mouth_upper_lip_right_contour1': {'x': 533, 'y': 726},\n",
       "    'mouth_upper_lip_right_contour2': {'x': 572, 'y': 736},\n",
       "    'mouth_upper_lip_right_contour3': {'x': 557, 'y': 754},\n",
       "    'mouth_upper_lip_top': {'x': 508, 'y': 733},\n",
       "    'nose_contour_left1': {'x': 470, 'y': 493},\n",
       "    'nose_contour_left2': {'x': 451, 'y': 605},\n",
       "    'nose_contour_left3': {'x': 469, 'y': 670},\n",
       "    'nose_contour_lower_middle': {'x': 505, 'y': 681},\n",
       "    'nose_contour_right1': {'x': 549, 'y': 493},\n",
       "    'nose_contour_right2': {'x': 564, 'y': 605},\n",
       "    'nose_contour_right3': {'x': 545, 'y': 670},\n",
       "    'nose_left': {'x': 438, 'y': 651},\n",
       "    'nose_right': {'x': 579, 'y': 652},\n",
       "    'nose_tip': {'x': 502, 'y': 642},\n",
       "    'right_eye_bottom': {'x': 643, 'y': 502},\n",
       "    'right_eye_center': {'x': 640, 'y': 484},\n",
       "    'right_eye_left_corner': {'x': 581, 'y': 493},\n",
       "    'right_eye_lower_left_quarter': {'x': 611, 'y': 498},\n",
       "    'right_eye_lower_right_quarter': {'x': 674, 'y': 496},\n",
       "    'right_eye_pupil': {'x': 644, 'y': 476},\n",
       "    'right_eye_right_corner': {'x': 697, 'y': 484},\n",
       "    'right_eye_top': {'x': 639, 'y': 458},\n",
       "    'right_eye_upper_left_quarter': {'x': 606, 'y': 469},\n",
       "    'right_eye_upper_right_quarter': {'x': 672, 'y': 465},\n",
       "    'right_eyebrow_left_corner': {'x': 547, 'y': 458},\n",
       "    'right_eyebrow_lower_left_quarter': {'x': 599, 'y': 453},\n",
       "    'right_eyebrow_lower_middle': {'x': 649, 'y': 443},\n",
       "    'right_eyebrow_lower_right_quarter': {'x': 699, 'y': 438},\n",
       "    'right_eyebrow_right_corner': {'x': 748, 'y': 436},\n",
       "    'right_eyebrow_upper_left_quarter': {'x': 592, 'y': 428},\n",
       "    'right_eyebrow_upper_middle': {'x': 644, 'y': 415},\n",
       "    'right_eyebrow_upper_right_quarter': {'x': 700, 'y': 411}},\n",
       "   'attributes': {'gender': {'value': 'Male'},\n",
       "    'age': {'value': 26},\n",
       "    'smile': {'value': 0.0, 'threshold': 50.0}}}],\n",
       " 'image_id': 'mmFZQkDFSW6zdlcctrYglA==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "91f5c8c2",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1.导入需要的requests模块\n",
    "import requests\n",
    "\n",
    "# 2.输入我们需要API（face++）网站注册的API_Key\\Secret\n",
    "\n",
    "API_Key = \"aZW8w-JCfLUxYnTwQQu_RKLAUduXKvTW\"\n",
    "API_Secret = \"wmAcTGRDAkP1JB4zYe0H3Q15mn1UCMxC\"\n",
    "\n",
    "# 3.准备base URL(face detect) API测试接口的url\n",
    "base_url = \"https://api-cn.faceplusplus.com/facepp/v3/compare\"\n",
    "\n",
    "# 4.请求方法：POST\n",
    "\n",
    "# 5.准备请求参数：payload\n",
    "payload = {\n",
    "    \"api_key\":API_Key,\n",
    "    \"api_secret\":API_Secret,\n",
    "    # 两个不同或者同一个人的两张不同照片对比是否为同一个人，按上面的方法找出facce_token\n",
    "    \"face_token1\":'ede4e42e62954a8439844d49ce50fc9e',\n",
    "    \"face_token2\": 'ce2c77d7c093ebbd320a8bdfa7ed8340',\n",
    "    \"image_url\":\"http://thumb10.jfcdns.com/thumb/up/2017-8/201782115518958221215010_600_0.jpg\",\n",
    "    \"return_landmark\":1,\n",
    "    \"return_attributes\":\"gender,age,smiling\"\n",
    "\n",
    "}\n",
    "# 6.请求数据\n",
    "\n",
    "r = requests.post(base_url,payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "8e78625f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "c21610f8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647315204,a3af617e-7ae2-4bd4-8c52-35d779ba3f9c',\n",
       " 'time_used': 475,\n",
       " 'confidence': 50.884,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-4': 69.101, '1e-5': 73.975}}"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0b09d073",
   "metadata": {},
   "source": [
    "## 创建人脸库API"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "1a999c22",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "\n",
    "# 2.输入我们需要API（face++）网站注册的API_Key\\Secret\n",
    "\n",
    "API_Key = \"aZW8w-JCfLUxYnTwQQu_RKLAUduXKvTW\"\n",
    "API_Secret = \"wmAcTGRDAkP1JB4zYe0H3Q15mn1UCMxC\"\n",
    "\n",
    "# 3.准备base URL(face detect) API测试接口的url\n",
    "base_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/create\"\n",
    "\n",
    "# 4.请求方法：POST\n",
    "\n",
    "# 5.准备请求参数：payload，face_tokens最多可以输入5个，先用人脸检索获取face_tokens\n",
    "payload = {\n",
    "    \"api_key\":API_Key,\n",
    "    \"api_secret\":API_Secret,\n",
    "    \"face_tokens\":\"ce2c77d7c093ebbd320a8bdfa7ed8340​,cced722224d8688284c8e098c362d036\",\n",
    "}\n",
    "# 6.请求数据\n",
    "\n",
    "r = requests.post(base_url,payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "ae0e09c7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "d186a1b5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': 'f245e3933579c4a851dbae3f49fd3119',\n",
       " 'time_used': 581,\n",
       " 'face_count': 1,\n",
       " 'face_added': 1,\n",
       " 'request_id': '1647583288,b79edd99-dc71-4fe4-9f5c-804ce7309ca7',\n",
       " 'outer_id': '',\n",
       " 'failure_detail': [{'reason': 'INVALID_FACE_TOKEN',\n",
       "   'face_token': 'ce2c77d7c093ebbd320a8bdfa7ed8340\\u200b'}]}"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 通过建立人脸库API可以获取人脸库faceset信息\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7a808b05",
   "metadata": {},
   "source": [
    "## 人脸搜索"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "8f1114cd",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "\n",
    "# 2.输入我们需要API（face++）网站注册的API_Key\\Secret\n",
    "\n",
    "API_Key = \"aZW8w-JCfLUxYnTwQQu_RKLAUduXKvTW\"\n",
    "API_Secret = \"wmAcTGRDAkP1JB4zYe0H3Q15mn1UCMxC\"\n",
    "\n",
    "# 3.准备base URL(face detect) API测试接口的url\n",
    "base_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/create\"\n",
    "\n",
    "# 4.请求方法：POST\n",
    "\n",
    "# 5.准备请求参数：payload,输入需要搜索的face_tokens,以及人脸库faceset地址\n",
    "payload = {\n",
    "    \"api_key\":API_Key,\n",
    "    \"api_secret\":API_Secret,\n",
    "    \"face_tokens\":\"ce2c77d7c093ebbd320a8bdfa7ed8340\",\n",
    "    \"faceset_token\":\"619ea6aa135915d8bc904600704853ad\"\n",
    "}\n",
    "# 6.请求数据\n",
    "\n",
    "r = requests.post(base_url,payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "7762e4e5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "7418f6c4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': 'c9c0feb092c5d6f3ead409332b7b7e6b',\n",
       " 'time_used': 141,\n",
       " 'face_count': 0,\n",
       " 'face_added': 0,\n",
       " 'request_id': '1647582950,6d9a5cc5-a401-460e-bc9e-14af629087ba',\n",
       " 'outer_id': '',\n",
       " 'failure_detail': [{'reason': 'INVALID_FACE_TOKEN',\n",
       "   'face_token': 'ce2c77d7c093ebbd320a8bdfa7ed8340'}]}"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "667fb7c2",
   "metadata": {},
   "source": [
    "# 封装应用函数（内部使用的接口）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e101ea71",
   "metadata": {},
   "source": [
    "##   人脸检测"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "6e8164de",
   "metadata": {},
   "outputs": [],
   "source": [
    "def face_detect(API_key,API_secret,image_url,landmark=None,attributes=None):\n",
    "    \"\"\"face++ 人脸检测\"\"\"\n",
    "    import requests\n",
    "    url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "    payload ={\n",
    "        'api_key':API_key,\n",
    "        'api_secret':API_secret,\n",
    "        'image_url':image_url,\n",
    "        'return_landmark':landmark,\n",
    "        'return_attributes':attributes\n",
    "    }\n",
    "    r = requests.post(url,params=payload)\n",
    "    return r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "9bb70937",
   "metadata": {},
   "outputs": [],
   "source": [
    "face_a = face_detect('aZW8w-JCfLUxYnTwQQu_RKLAUduXKvTW','wmAcTGRDAkP1JB4zYe0H3Q15mn1UCMxC','http://thumb10.jfcdns.com/thumb/up/2017-8/201782115518958221215010_600_0.jpg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "66f6c663",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'03bc6283e623e5c207a603b9f400534e'"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_a['faces'][0]['face_token']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "841e16d2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647583829,6af1094f-89ab-4e36-8f15-ee4089f1491b',\n",
       " 'time_used': 995,\n",
       " 'faces': [{'face_token': '0aaea0e097b5e0a21e7935839a7f7577',\n",
       "   'face_rectangle': {'top': 132, 'left': 178, 'width': 174, 'height': 174}}],\n",
       " 'image_id': 'iGkOO0vIVdOtC8bDQWAVmA==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_detect('aZW8w-JCfLUxYnTwQQu_RKLAUduXKvTW','wmAcTGRDAkP1JB4zYe0H3Q15mn1UCMxC','http://thumb10.jfcdns.com/thumb/up/2017-8/201782115518958221215010_600_0.jpg')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7742774e",
   "metadata": {},
   "source": [
    "## 人脸分析"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "defaaf78",
   "metadata": {},
   "outputs": [],
   "source": [
    "def face_analyze(API_key,API_secret,face_tokens,return_landmark):\n",
    "    \"\"\"face++ 人脸检测\"\"\"\n",
    "    import requests\n",
    "    url = 'https://api-cn.faceplusplus.com/facepp/v3/face/analyze'\n",
    "    payload = {\n",
    "    \"api_key\":API_Key,\n",
    "    \"api_secret\":API_Secret,\n",
    "    \"face_tokens\":face_token,\n",
    "    \"return_landmark\":landmark,\n",
    "    \"return_attributes\":attributes\n",
    "}\n",
    "    r = requests.post(url,params=payload)\n",
    "    return r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "7b5905e4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647584276,9ec63049-7199-47b9-b21a-373eddf918a1',\n",
       " 'time_used': 62,\n",
       " 'error_message': 'INVALID_IMAGE_URL'}"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_detect('aZW8w-JCfLUxYnTwQQu_RKLAUduXKvTW','wmAcTGRDAkP1JB4zYe0H3Q15mn1UCMxC','03bc6283e623e5c207a603b9f400534e','1')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5a506988",
   "metadata": {},
   "source": [
    "## 人脸对比"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "692daae4",
   "metadata": {},
   "outputs": [],
   "source": [
    "def face_compare(API_key,API_secret,face_token1,face_token2):\n",
    "    \"\"\"face++ 人脸检测\"\"\"\n",
    "    import requests\n",
    "    url = 'https://api-cn.faceplusplus.com/facepp/v3/compare'\n",
    "    payload = {\n",
    "    \"api_key\":API_Key,\n",
    "    \"api_secret\":API_Secret,\n",
    "    \"face_token1\":face_token1,\n",
    "    \"face_token2\":face_token2,\n",
    "}\n",
    "    r = requests.post(url,params=payload)\n",
    "    return r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "433edf32",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647584572,dc8aa3fd-577c-4881-97b1-56b13b866b4a',\n",
       " 'time_used': 104,\n",
       " 'error_message': \"INVALID_FACE_TOKEN: ['f245e3933579c4a851dbae3f49fd3119']\"}"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_compare('aZW8w-JCfLUxYnTwQQu_RKLAUduXKvTW','wmAcTGRDAkP1JB4zYe0H3Q15mn1UCMxC','03bc6283e623e5c207a603b9f400534e','f245e3933579c4a851dbae3f49fd3119')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b0e92630",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
